﻿@model AffiliateListModel
@inject AdminAreaSettings adminAreaSettings
@{
    //page title
    ViewBag.Title = T("Admin.Affiliates").Text;
}

<div class="row">
    <div class="col-md-12">
        <div class="x_panel light form-fit">
            <div class="x_title">
                <div class="caption">
                    <i class="fa fa-cube"></i>
                    @T("Admin.Affiliates")
                </div>
                <div class="actions btn-group btn-group-devided">
                    <a href="@Url.Action("Create")" class="btn green"><i class="fa fa-plus"></i><span class="hidden-xs"> @T("Admin.Common.AddNew") </span></a>
                </div>
            </div>
            <vc:admin-widget widget-zone="affiliate_list_buttons" additional-data="null" />
            <div class="x_content form">
                <div class="form-horizontal">
                    <div class="form-body">
                        <div class="form-group">
                            <admin-label asp-for="SearchFirstName" />
                            <div class="col-md-9 col-sm-9">
                                <admin-input asp-for="SearchFirstName" />
                            </div>
                        </div>
                        <div class="form-group">
                            <admin-label asp-for="SearchLastName" />
                            <div class="col-md-9 col-sm-9">
                                <admin-input asp-for="SearchLastName" />
                            </div>
                        </div>
                        <div class="form-group">
                            <admin-label asp-for="SearchFriendlyUrlName" />
                            <div class="col-md-9 col-sm-9">
                                <admin-input asp-for="SearchFriendlyUrlName" />
                            </div>
                        </div>
                        <div class="form-group">
                            <admin-label asp-for="LoadOnlyWithOrders" />
                            <div class="col-md-9 col-sm-9">
                                <label class="mt-checkbox mt-checkbox-outline control control-checkbox">
                                    <admin-input asp-for="LoadOnlyWithOrders" />
                                     <div class="control__indicator"></div>
                                </label>
                            </div>
                        </div>
                        <div class="form-group" id="pnlOrdersCreatedFromUtc">
                            <admin-label asp-for="OrdersCreatedFromUtc" />
                            <div class="col-md-9 col-sm-9">
                                <admin-input asp-for="OrdersCreatedFromUtc" />
                            </div>
                        </div>
                        <div class="form-group" id="pnlOrdersCreatedToUtc">
                            <admin-label asp-for="OrdersCreatedToUtc" />
                            <div class="col-md-9 col-sm-9">
                                <admin-input asp-for="OrdersCreatedToUtc" />
                            </div>
                        </div>
                    </div>
                    <div class="form-actions">
                        <div class="row">
                            <div class="col-md-offset-3 col-sm-offset-3 col-md-9 col-sm-9">
                                <button class="btn btn-success filter-submit" id="search-affiliates">
                                    <i class="fa fa-search"></i> @T("Admin.Common.Search")
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="x_content">
                <div id="affiliates-grid"></div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    $(document).ready(function () {
        $("#@Html.FieldIdFor(model => model.LoadOnlyWithOrders)").click(toggleLoadOnlyWithOrders);

        toggleLoadOnlyWithOrders();
    });

    function toggleLoadOnlyWithOrders() {
        if ($('#@Html.FieldIdFor(model => model.LoadOnlyWithOrders)').is(':checked')) {
            $('#pnlOrdersCreatedFromUtc').show();
            $('#pnlOrdersCreatedToUtc').show();
        } else {
            $('#pnlOrdersCreatedFromUtc').hide();
            $('#pnlOrdersCreatedToUtc').hide();
        }
    }
</script>


<script>
        $(document).ready(function () {
            $("#affiliates-grid").kendoGrid({
                dataSource: {
                    transport: {
                        read: {
                            url: "@Html.Raw(Url.Action("List", "Affiliate"))",
                            type: "POST",
                            dataType: "json",
                            data: additionalData
                        }
                    },
                    schema: {
                        data: "Data",
                        total: "Total",
                        errors: "Errors"
                    },
                    error: function(e) {
                        display_kendoui_grid_error(e);
                        // Cancel the changes
                        this.cancelChanges();
                    },
                    pageSize: @(adminAreaSettings.DefaultGridPageSize),
                    serverPaging: true,
                    serverFiltering: true,
                    serverSorting: true
                },
                pageable: {
                    refresh: true,
                    pageSizes: [@(adminAreaSettings.GridPageSizes)]
                },
                editable: {
                    confirmation: false,
                    mode: "inline"
                },
                scrollable: false,
                columns: [
                {
                    field: "Address.Email",
                    title: "@T("Admin.Address.Fields.Email")",
                    width: 200,
                    template: '<a class="k-link" href="Edit/#=Id#">#=Address.Email#</a>',
                }, {
                    field: "Address.FirstName",
                    title: "@T("Admin.Address.Fields.FirstName")",
                    width: 200,
                }, {
                    field: "Address.LastName",
                    title: "@T("Admin.Address.Fields.LastName")",
                    width: 200
                }, {
                    field: "Active",
                    title: "@T("Admin.Affiliates.Fields.Active")",
                    width: 100,
                    headerAttributes: { style: "text-align:center" },
                    attributes: { style: "text-align:center" },
                    template: '# if(Active) {# <i class="fa fa-check" aria-hidden="true" style="color:green"></i> #} else {# <i class="fa fa-times" aria-hidden="true" style="color:red"></i> #} #'
                }]
            });
        });
</script>
<script type="text/javascript">
    $(document).ready(function () {

        $('#search-affiliates').click(function () {
            var grid = $('#affiliates-grid').data('kendoGrid');
            grid.dataSource.page(1); //new search. Set page size to 1
            //grid.dataSource.read(); we already loaded the grid above using "page" function
            return false;
        });
    });

    $("#@Html.FieldIdFor(model => model.SearchFirstName)").keydown(function (event) {
        if (event.keyCode == 13) {
            $("#search-affiliates").click();
            return false;
        }
    });
    $("#@Html.FieldIdFor(model => model.SearchLastName)").keydown(function (event) {
        if (event.keyCode == 13) {
            $("#search-affiliates").click();
            return false;
        }
    });
    $("#@Html.FieldIdFor(model => model.SearchFriendlyUrlName)").keydown(function (event) {
        if (event.keyCode == 13) {
            $("#search-affiliates").click();
            return false;
        }
    });

    function additionalData() {
        var data = {
            SearchFirstName: $('#@Html.FieldIdFor(model => model.SearchFirstName)').val(),
            SearchLastName: $('#@Html.FieldIdFor(model => model.SearchLastName)').val(),
            SearchFriendlyUrlName: $('#@Html.FieldIdFor(model => model.SearchFriendlyUrlName)').val(),
            LoadOnlyWithOrders: $('#@Html.FieldIdFor(model => model.LoadOnlyWithOrders)').is(':checked'),
            OrdersCreatedFromUtc: $('#@Html.FieldIdFor(model => model.OrdersCreatedFromUtc)').val(),
            OrdersCreatedToUtc: $('#@Html.FieldIdFor(model => model.OrdersCreatedToUtc)').val()
        };
        addAntiForgeryToken(data);
        return data;
    }

</script>
